package com.sztx.pay.center.rpc.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sztx.pay.center.common.annotation.SlaveQueryAnnotation;
import com.sztx.pay.center.common.util.ValidateUtil;
import com.sztx.pay.center.dataaccess.dao.mysql.RiskTradeTypeMysqlDAO;
import com.sztx.pay.center.dataaccess.domain.RiskTradeTypeDO;
import com.sztx.pay.center.rpc.api.domain.PageModelResponse;
import com.sztx.pay.center.rpc.api.domain.RiskTradeTypeRequest;
import com.sztx.pay.center.rpc.api.domain.RiskTradeTypeResponse;
import com.sztx.pay.center.rpc.api.service.RiskTradeTypeRpcService;
import com.sztx.se.common.exception.BusinessException;
import com.sztx.se.dataaccess.mysql.config.PageQuery;
@Service
public class RiskTradeTypeRpcServiceImpl implements RiskTradeTypeRpcService{
	@Autowired
	RiskTradeTypeMysqlDAO riskTradeTypeMysqlDAO;
	
	
	@Override
	public void saveTradeType(RiskTradeTypeRequest request) {
		checkParam(request);
		RiskTradeTypeDO selectTradeTypeDO = riskTradeTypeMysqlDAO.findByTradeTypeId(request.getTradeTypeId());
		if(selectTradeTypeDO != null){
			throw new BusinessException("业务类型ID已存在,不允许重复添加");
		}
		RiskTradeTypeDO riskTradeTypeDO = new RiskTradeTypeDO();
		riskTradeTypeDO.setCreateUser(request.getUser());
		riskTradeTypeDO.setRemark(request.getRemark());
		riskTradeTypeDO.setStatus(request.getStatus());
		riskTradeTypeDO.setTradeTypeId(request.getTradeTypeId());
		riskTradeTypeDO.setTradeTypeName(request.getTradeTypeName());
		riskTradeTypeMysqlDAO.save(riskTradeTypeDO);
	}

	@Override
	public void updateTradeType(RiskTradeTypeRequest request) {
		checkParam(request);
		RiskTradeTypeDO selectTradeTypeDO = riskTradeTypeMysqlDAO.findByTradeTypeId(request.getTradeTypeId());
		if(selectTradeTypeDO == null){
			throw new BusinessException("业务类型ID不存在,不允许更新");
		}
		RiskTradeTypeDO riskTradeTypeDO = new RiskTradeTypeDO();
		riskTradeTypeDO.setUpdateUser(request.getUser());
		riskTradeTypeDO.setRemark(request.getRemark());
		riskTradeTypeDO.setStatus(request.getStatus());
		riskTradeTypeDO.setTradeTypeId(request.getTradeTypeId());
		riskTradeTypeDO.setTradeTypeName(request.getTradeTypeName());
		riskTradeTypeMysqlDAO.update(riskTradeTypeDO, request.getTradeTypeId());
	}

	@Override
	@SlaveQueryAnnotation
	public PageModelResponse<RiskTradeTypeResponse> queryTradeTypeList(
			RiskTradeTypeRequest request,int pageNo,int pageSize) {
		ValidateUtil.ge(pageNo, 1, "pageNo必须大于0");
		ValidateUtil.ge(pageSize, 1, "pageSize必须大于0");
		
		PageQuery pageQuery = new PageQuery(pageNo, pageSize);
		RiskTradeTypeDO riskTradeTypeDO = new RiskTradeTypeDO();
		riskTradeTypeDO.setStatus(request.getStatus());
		riskTradeTypeDO.setTradeTypeId(request.getTradeTypeId());
		riskTradeTypeDO.setTradeTypeName(request.getTradeTypeName());
		Integer listCount = riskTradeTypeMysqlDAO.listCount(riskTradeTypeDO);
		PageModelResponse<RiskTradeTypeResponse> response = new PageModelResponse<RiskTradeTypeResponse>();
		List<RiskTradeTypeResponse> list = new ArrayList<RiskTradeTypeResponse>();
		if(listCount > 0){
			List<RiskTradeTypeDO> riskTradeTypeDOList = riskTradeTypeMysqlDAO.findAllBussinessType(riskTradeTypeDO , pageQuery);
			for (RiskTradeTypeDO riskTradeTypeDO2 : riskTradeTypeDOList) {
				RiskTradeTypeResponse tmp = new RiskTradeTypeResponse();
				tmp.setCreateTime(riskTradeTypeDO2.getCreateTime());
				tmp.setCreateUser(riskTradeTypeDO2.getCreateUser());
				tmp.setRemark(riskTradeTypeDO2.getRemark());
				tmp.setStatus(riskTradeTypeDO2.getStatus());
				tmp.setTradeTypeId(riskTradeTypeDO2.getTradeTypeId());
				tmp.setTradeTypeName(riskTradeTypeDO2.getTradeTypeName());
				tmp.setUpdateTime(riskTradeTypeDO2.getUpdateTime());
				tmp.setUpdateUser(riskTradeTypeDO2.getUpdateUser());
				list.add(tmp);
			}
		}
		
		response.setList(list);
		response.setPageNo(pageNo);
		response.setPageSize(pageSize);
		response.setTotal(listCount);
		return response;
	}
	private void checkParam(RiskTradeTypeRequest request){
		ValidateUtil.isNotBlank(request.getTradeTypeId(), "业务类型编号不能为空");
		ValidateUtil.isNotBlank(request.getTradeTypeName(), "业务类型名称不能为空");
		ValidateUtil.isBetween(request.getStatus(), 0, 1, "业务类型状态错误");
	}
}
